package com.test;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class HelloSwing extends JFrame {

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		new HelloSwing().run();
	}

	/**
	 * 运行程序
	 */
	private void run() {
		configureJFrame();
		createButton();

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		setVisible(true);
	}

	/**
	 * 创建主窗体
	 */
	private void configureJFrame() {
		setTitle("Hello Swing");
		getContentPane().setLayout(new FlowLayout());
		setSize(new Dimension(200, 200));
		setLocation(0, 0);
	}

	/**
	 * 创建按钮
	 */
	private void createButton() {
		Button button = new Button("打开");

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createDialog();
			}
		});

		getContentPane().add(button, BorderLayout.NORTH);
	}

	/**
	 * 创建对话框
	 */
	private void createDialog() {
		JOptionPane.showMessageDialog(HelloSwing.this, "这是一个对话框",
				"对话框", JOptionPane.INFORMATION_MESSAGE);
	}
}
